'''
获取文本编码格式
'''

from chardet.universaldetector import UniversalDetector
import chardet
def GetEncoding(file):
    """
    获取文本文件的编码类型
    :param file:
    :return: 返回值是字典 {'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}
    """
    txt = open(file, "rb")
    detector = UniversalDetector()
    for line in txt.readlines():
        detector.feed(line)
        if detector.done:
            print(line,type(line))

            break
            # pass
    detector.close()
    txt.close()
    return detector.result

if __name__ == '__main__':
    r = GetEncoding('bb211222b0.csv')
    print(f'{r} {type(r)}')

    # f = open('bb211222b0.csv', 'rb')  #以二进制方式读取文件
    # str1 = f.read()
    # char_encoding= chardet.detect(str1)
    # print(f'该字符串为：{str1}')
    # print(f'该字符串编码信息为：{char_encoding}' ) 
    # print(f'该字符串编码为： {char_encoding["encoding"]}')